capture log close
log 	using 20250823-brfss2015-MI-dataprep-afterimpute, replace text nomsg
version 15.1
clear 	all
macro 	drop _all
set 	linesize 80
set		more off
set		varabbrev off
set 	rmsg on

// #0. Document this do-file and set up packages and directories
	local today : di %tdCY/N/D daily("$S_DATE", "DMY")
	tokenize `today',parse("/")
	local year `1'
	local month `3'
	local day `5'

	local who "Ben Cheung"
	local pgm "20250823-brfss2015-MI-dataprep-afterimpute"
	local date "`year'-`month'-`day'"
	local tag "`pgm'.do by `who' on `date'"
	display "The tag is: `tag'"


	local today : di %tdCYND daily("$S_DATE", "DMY")

	*Check for installed packages
	capture which scheme-plottig.scheme
	if _rc!=0 ssc install blidschemes

	capture which confirmdir
	if _rc!=0 ssc install confirmdir

	capture which estout
	if _rc!=0 ssc install estout

	capture which sum2docx
	if _rc!=0 ssc install sum2docx

	capture which tab2xl
	if _rc!=0 net install http://www.stata.com/users/kcrow/tab2xl, replace

	set scheme plottig




local folder "Data Ready for Analysis"
confirmdir "`folder'"
if `r(confirmdir)'==170 {
	mkdir "`folder'"
}

local folder "graph"
confirmdir "`folder'"
if `r(confirmdir)'==170 {
	mkdir "`folder'"
}

local folder "model"
confirmdir "`folder'"
if `r(confirmdir)'==170 {
	mkdir "`folder'"
}

local folder "summary"
confirmdir "`folder'"
if `r(confirmdir)'==170 {
	mkdir "`folder'"
}


program define pe
	version 15.1
	if `"`0'"' != "" {
		display as input `"`0'"'
		`0'
		display("")
	}
end



// #1. Data management
use "Data Ready for Analysis/brfss2015-readyforimpute.dta",clear
keep id psu strata state //Somehow the state FIPS code in R is messed up. I drop state in R after MI and then merge from here



merge 1:m id using "Data Ready for Analysis/brfss2015-impute-10.dta", nogen


*Somehow the categorical variables in R are counted from 1 rather than 0 (I count from 0)
local varlist exercise exercise1type exercise2type exe_intensity_simplest exe_active exe_aerobic_advice exe_strength_advice male educ praceethnicgp_multi_sim hhincome ///
srh badphyhlth badmenhlth bmicat employstatus_full
foreach var of local varlist {
	replace `var'=`var'-1
}


*Drop observations
drop if state==72 //delete Puerto Rico

drop if yearsold<25

gen year=2015

save "Data Ready for Analysis/brfss2015-impute-10-merged.dta",replace


mi import flong, m(m_num) id(id) imputed(exercise exercise1type exercise2type ///
exe_intensity_simplest exe_aerobic_advice exe_strength_advice ///
male educ praceethnicgp_multi_sim hhincome ///
srh badphyhlth badmenhlth bmicat employstatus_full)

mi register regular strata psu id column_label svyweight state m_num




local new "censusdivision"
local old "state"
gen `new'=0 if `old'==9 | `old'==23 | `old'==25 | `old'==33 | `old'==44 | `old'==50
replace `new'=1 if `old'==34 | `old'==36 | `old'==42
replace `new'=2 if `old'==17 | `old'==18 | `old'==26 | `old'==39 | `old'==55
replace `new'=3 if `old'==19 | `old'==20 | `old'==27 | `old'==29 | `old'==31 | `old'==38 | `old'==46
replace `new'=4 if `old'==12 | `old'==13 | `old'==37 | `old'==45 | `old'==51 | `old'==11 | `old'==24 | `old'==10 | `old'==54
replace `new'=5 if `old'==1  | `old'==21 | `old'==28 | `old'==47
replace `new'=6 if `old'==5 | `old'==22 | `old'==40 | `old'==48
replace `new'=7 if `old'==4 | `old'==8 | `old'==16 | `old'==30 | `old'==32 | `old'==35 | `old'==49 | `old'==56
replace `new'=8 if `old'==2 | `old'==6 | `old'==15 | `old'==41 | `old'==53
replace `new'=9 if `old'==66
replace `new'=10 if `old'==72
label var `new' "Census Division"
label define `new' 0"NewEngland" 1"MidAtlantic" 2"ENCentral" 3"WNCentral" 4"SAtlantic" 5"ESCentral" 6"WSCentral" 7"Mountain" 8"Pacific" 9"Guam" 10"PuertoRico"
numlabel `new', mask(#) add
label value `new' `new'
note `new': Original: `old' | `tag'


local new "censusregion"
local old "censusdivision"
gen `new'=0 if `old'>=0 & `old'<=1
replace `new'=1 if `old'>=2 & `old'<=3
replace `new'=2 if `old'>=4 & `old'<=6
replace `new'=3 if `old'>=7 & `old'<=8
replace `new'=4 if `old'==9
replace `new'=5 if `old'==10
label var `new' "Census Region"
label define `new' 0"Northeast" 1"MidWest" 2"South" 3"West" 4"Guam" 5"PuertoRico"
numlabel `new', mask(#) add
label value `new' `new'
note `new': Original: `old' | `tag'


local new "censusregion_sim"
local old "censusregion"
gen `new'=0 if `old'>=0 & `old'<=2
replace `new'=1 if `old'==3 & state!=15
replace `new'=2 if state==15 //Put Hawaii into a separate category
replace `new'=3 if `old'==4
replace `new'=4 if `old'==5
label var `new' "Census Region"
label define `new' 0"Eastern" 1"West" 2"Hawaii" 3"Guam" 4"PuertoRico"
numlabel `new', mask(#) add
label value `new' `new'
note `new': Original: `old' | `tag'




save "Data Ready for Analysis/brfss2015-MI-pathanalysis-sem-flong.dta",replace

*make the outcome variables missing in the imputed datasets if they are missing in the original data
mi convert wide


local varlist exe1_intensity exe2_intensity exe_intensity_simplest exe_aerobic_advice exe_strength_advice exe_active
foreach var of local varlist {
	gen `var'_miss=0
	replace `var'_miss=1 if mi(`var')
}

save "Data Ready for Analysis/brfss2015-MI-pathanalysis-sem-wide.dta",replace

mi convert flong


local varlist exe1_intensity exe2_intensity exe_intensity_simplest exe_aerobic_advice exe_strength_advice exe_active
foreach var of local varlist {
	mi xeq: replace `var'=. if `var'_miss==1
}



*For gsem estimation
mi passive: gen exe_intensity_simplest_m=exe_intensity_simplest if male==1
mi passive: gen exe_intensity_simplest_f=exe_intensity_simplest if male==0

mi passive: gen exe_aerobic_advice_m=exe_aerobic_advice if male==1
mi passive: gen exe_aerobic_advice_f=exe_aerobic_advice if male==0

mi passive: gen exe_strength_advice_m=exe_strength_advice if male==1
mi passive: gen exe_strength_advice_f=exe_strength_advice if male==0

mi passive: gen exe_active_m=exe_active if male==1
mi passive: gen exe_active_f=exe_active if male==0


local dvlist exe_aerobic_advice_m exe_aerobic_advice_f
local dvlist `dvlist' exe_strength_advice_m exe_strength_advice_f exe_active_m exe_active_f exe_intensity_simplest_m exe_intensity_simplest_f
foreach var in `dvlist' {
	forvalues dvnum=1(1)5 {
		mi passive: gen `var'`dvnum'=`var'
	}
}





*Generate outcomes by census region
mi passive: gen exe_intensity_simplest_m_cr0=exe_intensity_simplest_m if censusregion_sim==0
mi passive: gen exe_intensity_simplest_m_cr1=exe_intensity_simplest_m if censusregion_sim==1
mi passive: gen exe_intensity_simplest_m_cr2=exe_intensity_simplest_m if censusregion_sim==2
mi passive: gen exe_intensity_simplest_m_cr3=exe_intensity_simplest_m if censusregion_sim==3
mi passive: gen exe_intensity_simplest_m_cr4=exe_intensity_simplest_m if censusregion_sim==4

mi passive: gen exe_intensity_simplest_f_cr0=exe_intensity_simplest_f if censusregion_sim==0
mi passive: gen exe_intensity_simplest_f_cr1=exe_intensity_simplest_f if censusregion_sim==1
mi passive: gen exe_intensity_simplest_f_cr2=exe_intensity_simplest_f if censusregion_sim==2
mi passive: gen exe_intensity_simplest_f_cr3=exe_intensity_simplest_f if censusregion_sim==3
mi passive: gen exe_intensity_simplest_f_cr4=exe_intensity_simplest_f if censusregion_sim==4





mi passive: gen exe_aerobic_advice_m_cr0=exe_aerobic_advice_m if censusregion_sim==0
mi passive: gen exe_aerobic_advice_m_cr1=exe_aerobic_advice_m if censusregion_sim==1
mi passive: gen exe_aerobic_advice_m_cr2=exe_aerobic_advice_m if censusregion_sim==2
mi passive: gen exe_aerobic_advice_m_cr3=exe_aerobic_advice_m if censusregion_sim==3
mi passive: gen exe_aerobic_advice_m_cr4=exe_aerobic_advice_m if censusregion_sim==4

mi passive: gen exe_aerobic_advice_f_cr0=exe_aerobic_advice_f if censusregion_sim==0
mi passive: gen exe_aerobic_advice_f_cr1=exe_aerobic_advice_f if censusregion_sim==1
mi passive: gen exe_aerobic_advice_f_cr2=exe_aerobic_advice_f if censusregion_sim==2
mi passive: gen exe_aerobic_advice_f_cr3=exe_aerobic_advice_f if censusregion_sim==3
mi passive: gen exe_aerobic_advice_f_cr4=exe_aerobic_advice_f if censusregion_sim==4

mi passive: gen exe_strength_advice_m_cr0=exe_strength_advice_m if censusregion_sim==0
mi passive: gen exe_strength_advice_m_cr1=exe_strength_advice_m if censusregion_sim==1
mi passive: gen exe_strength_advice_m_cr2=exe_strength_advice_m if censusregion_sim==2
mi passive: gen exe_strength_advice_m_cr3=exe_strength_advice_m if censusregion_sim==3
mi passive: gen exe_strength_advice_m_cr4=exe_strength_advice_m if censusregion_sim==4

mi passive: gen exe_strength_advice_f_cr0=exe_strength_advice_f if censusregion_sim==0
mi passive: gen exe_strength_advice_f_cr1=exe_strength_advice_f if censusregion_sim==1
mi passive: gen exe_strength_advice_f_cr2=exe_strength_advice_f if censusregion_sim==2
mi passive: gen exe_strength_advice_f_cr3=exe_strength_advice_f if censusregion_sim==3
mi passive: gen exe_strength_advice_f_cr4=exe_strength_advice_f if censusregion_sim==4


mi passive: gen exe_active_m_cr0=exe_active_m if censusregion_sim==0
mi passive: gen exe_active_m_cr1=exe_active_m if censusregion_sim==1
mi passive: gen exe_active_m_cr2=exe_active_m if censusregion_sim==2
mi passive: gen exe_active_m_cr3=exe_active_m if censusregion_sim==3
mi passive: gen exe_active_m_cr4=exe_active_m if censusregion_sim==4

mi passive: gen exe_active_f_cr0=exe_active_f if censusregion_sim==0
mi passive: gen exe_active_f_cr1=exe_active_f if censusregion_sim==1
mi passive: gen exe_active_f_cr2=exe_active_f if censusregion_sim==2
mi passive: gen exe_active_f_cr3=exe_active_f if censusregion_sim==3
mi passive: gen exe_active_f_cr4=exe_active_f if censusregion_sim==4



mi svyset [pweight=svyweight], strata(strata) psu(psu) singleunit(centered)








compress

note: Dataset with analyses results done using `pgm'.do by `who' on `date'.

save "Data Ready for Analysis/brfss2015-impute-10-readyforanalysis.dta",replace







log close
exit

